package com.smalldoor.thread.demo01;


import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//下载文件
public class Thread02 implements Runnable{

    private String name;

    private String url;

    public Thread02(String url,String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownload webDownload = new WebDownload();
        webDownload.downloadFile(url,name);
        System.out.println("文件已下载，文件名为：" + name);
    }

    public static void main(String[] args) {
        Thread02 t1 = new Thread02("https://pic2.zhimg.com/v2-5fb13110e1de13d4c11e6e7f5b8026da_r.jpg","1.jpg");
        Thread02 t2 = new Thread02("https://img.zcool.cn/community/01a88459ae0d88a801211d250cb394.jpg@1280w_1l_2o_100sh.jpg","2.jpg");
        Thread02 t3 = new Thread02("https://img.zcool.cn/community/018fcb55c2ef8d6ac7253f36f6d34e.jpg@1280w_1l_2o_100sh.jpg","3.jpg");

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }

    class WebDownload {

        public void downloadFile(String url, String name) {
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                System.out.println("io异常");
                throw new RuntimeException(e);
            }
        }
    }
}
